package com.ctsi.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel("接口返回对象")
@SuppressWarnings("unchecked")
public class ResultVO<T> implements Serializable {

    private static final long serialVersionUID = -1L;

    @ApiModelProperty("响应编码")
    private String resultCode;
    @ApiModelProperty("响应消息")
    private String resultMsg;
    @ApiModelProperty("响应结果")
    private T resultData;

    /**
     * 默认的无参构造器
     */
    public ResultVO() {

    }

    /**
     * 含有errorCode的构造器
     *
     * @param errorCode
     */
    public ResultVO(ErrorCode errorCode) {
        this.resultCode = errorCode.getCode();
        this.resultMsg = errorCode.getDesc();
    }

    /**
     * 含有resCode、resMessage的构造器
     *
     */
    public ResultVO(String resCode, String resMessage) {
        this.resultCode = resCode;
        this.resultMsg = resMessage;
    }

    /**
     * 含有resCode、resMessage、resultData的构造器
     *
     */
    public ResultVO(String resCode, String resMessage, T resultData) {
        this.resultCode = resCode;
        this.resultMsg = resMessage;
        this.resultData = resultData;
    }

    /**
     * 含有errorCode、resultData的构造器
     *
     * @param errorCode
     * @param resultData
     */
    public ResultVO(ErrorCode errorCode, T resultData) {
        this.resultCode = errorCode.getCode();
        this.resultMsg = errorCode.getDesc();
        this.resultData = resultData;
    }

    /**
     * 返回成功
     * @param <T>
     * @return
     */
    public static <T> ResultVO<T> success() {
        return new ResultVO(ErrorCode.SUCCESS);
    }

    /**
     * 返回成功
     * @param resMessage
     * @param <T>
     * @return
     */
    public static <T> ResultVO<T> success(String resMessage) {
        return new ResultVO(ErrorCode.SUCCESS.getCode(), resMessage);
    }

    /**
     * 返回成功
     * @param resultData
     * @param <T>
     * @return
     */
    public static <T> ResultVO<T> success(T resultData) {
        return new ResultVO(ErrorCode.SUCCESS, resultData);
    }

    /**
     * 返回成功
     * @param resMessage
     * @param resultData
     * @param <T>
     * @return
     */
    public static <T> ResultVO<T> success(String resMessage, T resultData) {
        return new ResultVO(ErrorCode.SUCCESS.getCode(), resMessage, resultData);
    }

    /**
     * 返回成功
     * @param resCode
     * @param resMessage
     * @param resultData
     * @param <T>
     * @return
     */
    public static <T> ResultVO<T> success(String resCode, String resMessage, T resultData) {
        return new ResultVO(resCode, resMessage, resultData);
    }

    /**
     * 返回失败
     * @param errorCode
     * @return
     */
    public static ResultVO error(ErrorCode errorCode) {
        return new ResultVO(errorCode);
    }

    /**
     * 返回失败
     * @param <T>
     * @return
     */
    public static <T> ResultVO<T> error() {
        return new ResultVO(ErrorCode.ERROR);
    }

    /**
     * 返回失败
     * @param errorMessage
     * @param <T>
     * @return
     */
    public static <T> ResultVO<T> error(String errorMessage) {
        return new ResultVO(ErrorCode.ERROR.getCode(), errorMessage);
    }

    /**
     * 返回失败
     * @param errorCode
     * @param errorMessage
     * @param <T>
     * @return
     */
    public static <T> ResultVO<T> error(String errorCode, String errorMessage) {
        return new ResultVO(errorCode, errorMessage);
    }


    @JsonIgnore
    public boolean isSuccess() {
        return ErrorCode.SUCCESS.getCode().equals(this.resultCode);
    }

}

